﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EYSLibrary;

namespace EYayincilikSistemi
{
     public partial class MakaleGonder : System.Web.UI.Page
     {
          protected void Page_Load(object sender, EventArgs e)
          {
               if (!IsPostBack)
               {
                    using (EYayincilikDBEntities eys = new EYayincilikDBEntities())
                    {
                         //Referans ekle checkbox listesini doldur

                         var makaleler = from Makale in eys.Makale
                                         where Makale.MakaleDurum == 1
                                         select Makale;

                         IList<Makale> makaleList = makaleler.ToList<Makale>();


                         CheckBoxList1.DataSource = makaleList;
                         CheckBoxList1.DataTextField = "Baslik";
                         CheckBoxList1.DataValueField = "MakaleId";
                         CheckBoxList1.DataBind();

                         //Dropdownlisti dergilerle doldur
                         var dergiler = from Dergi in eys.Dergi
                                        select Dergi;

                         IList<Dergi> dergiList = dergiler.ToList<Dergi>();


                         DropDownList1.DataSource = dergiList;
                         DropDownList1.DataTextField = "DergiAdi";
                         DropDownList1.DataValueField = "Id";
                         DropDownList1.DataBind();
                    }
               }
          }

          protected void Button1_Click(object sender, EventArgs e)
          {
               string baslik = TextBox1.Text;
               string metin = TextBox2.Text;
               int Dergi_ID = Convert.ToInt32(DropDownList1.SelectedItem.Value);

               Makale makale = new Makale();
               makale.Baslik = baslik;
               makale.MakaleDurum = 0;
               makale.Metin = metin;
               makale.YazarId = 1;
               makale.DergiId = Dergi_ID;

               using (EYayincilikDBEntities eys = new EYayincilikDBEntities())
               {
                    eys.Makale.AddObject(makale);
                    eys.SaveChanges();
               }
              
               for (int i = 0; i < CheckBoxList1.Items.Count; i++)
               {
                    if (CheckBoxList1.Items[i].Selected)
                    {
                         Referans referans= new Referans ();
                         referans.VerenMakaleId = Convert.ToInt32(CheckBoxList1.Items[i].Value);
                         referans.ReferansVerilenMakaleId = makale.MakaleId;
                         using (EYayincilikDBEntities eys = new EYayincilikDBEntities())
                         {
                              eys.Referans.AddObject(referans);
                              eys.SaveChanges();
                         }
                     }
                }
          }
     }
}